package com.example.server;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.aidl.ICatService;
import com.example.aidl.bean.Cat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AIDLService extends Service {
    private static String TAG = "MyAIDLService---->";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return myBinder;
    }

    private IBinder myBinder = new ICatService.Stub(){

        @Override
        public Cat getCat() throws RemoteException {
            return new Cat("tom", 100);
        }

        @Override
        public int getCatCount() throws RemoteException {
            return 10000;
        }

        @Override
        public void setCatPrice(Cat cat, int price) throws RemoteException {

        }

        @Override
        public void setCatName(Cat cat, String name) throws RemoteException {
            System.out.println("---------------->"+name);
        }

        //in 为定向 tag 的话表现为服务端将会接收到那个对象的完整数据，但是客户端的那个对象不会因为服务端对传参的修改而发生变动；
        @Override
        public void addCatIn(Cat cat) throws RemoteException {
            Log.d("", "In-->"+cat.toString()+"/"+cat.hashCode());//In-->Cat{name='t1', age=0, price=1}/26170806
            cat.name = "abc";
            cat.price = 101;
        }

        //out 的话表现为服务端将会接收到那个对象的参数为空的对象，但是在服务端对接收到的空对象有任何修改之后客户端将会同步变动；
        @Override
        public void addCatOut(Cat cat) throws RemoteException {
            Log.d("", "Out-->"+cat.toString()+"/"+cat.hashCode());//Out-->Cat{name='null', age=0, price=0}/115942071
            cat.name = "def";
            cat.price = 102;
        }

        //inout 为定向 tag 的情况下，服务端将会接收到客户端传来对象的完整信息，并且客户端将会同步服务端对该对象的任何变动。
        @Override
        public void addCatInout(Cat cat) throws RemoteException {
            Log.d("", "Inout-->"+cat.toString()+"/"+cat.hashCode());//Inout-->Cat{name='t3', age=0, price=3}/174176804
            cat.name = "hij";
            cat.price = 103;
        }

        @Override
        public void saveCat(Bundle bundle) throws RemoteException {
            bundle.setClassLoader(getClassLoader());
            Cat cat = bundle.getParcelable("cat");
            Log.d("", "saveCat-->"+cat.toString()+"/"+cat.hashCode());//saveCat-->Cat{name='t4', age=0, price=4}/45291330
        }

        @Override
        public void saveListCat(List<Cat> cats) throws RemoteException {
//            LinkedList<Cat> c = (LinkedList<Cat>) cats;
            ArrayList<Cat> c = (ArrayList<Cat>) cats;
            Log.d("", "saveListCat-->"+c);//saveListCat-->[Cat{name='a', age=0, price=44}, Cat{name='b', age=0, price=45}]
        }

        @Override
        public void saveMapCat(Map catMap) throws RemoteException {
//            Hashtable<String, Cat> m = (Hashtable<String, Cat>) catMap;
            HashMap<String, Cat> m = (HashMap<String, Cat>) catMap;
            Log.d("", "saveMapCat-->"+m);//saveMapCat-->{x=Cat{name='x', age=0, price=66}, y=Cat{name='y', age=0, price=67}, z=Cat{name='z', age=0, price=68}}
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: 服务创建");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: 服务销毁");
    }
}
